package com.avecsys.quotemanager.mbeans.supplier;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.model.SelectItem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.avecsys.quotemanager.dao.common.EnumActor;
import com.avecsys.quotemanager.exception.BusinessException;
import com.avecsys.quotemanager.mbeans.core.BaseMBean;
import com.avecsys.quotemanager.pojos.CountryMasterPojo;
import com.avecsys.quotemanager.pojos.SupplierMasterPojo;
import com.avecsys.quotemanager.service.common.CommonService;
import com.avecsys.quotemanager.service.supplier.SupplierMasterService;
import com.avecsys.quotemanager.utils.MessageBoardUtil;
import com.avecsys.quotemanager.utils.SelectItemUtils;
import com.avecsys.quotemanager.utils.TransactionStatus;

public class SupplierMasterMBean extends BaseMBean implements Serializable {

	private final Log log = LogFactory.getLog(SupplierMasterMBean.class);

	private SupplierMasterService supplierMasterService;

	private CommonService commonService;

	private SupplierMasterPojo supplierMasterPojo = new SupplierMasterPojo();

	private List<SelectItem> countrySelectItemList = new ArrayList<SelectItem>();

	private Object dummy;

	/**
	 * @return the dummy
	 */
	public Object getDummy() {
		return dummy;
	}

	/**
	 * @param dummy
	 *            the dummy to set
	 */
	public void setDummy(Object dummy) {
		this.dummy = dummy;
	}

	public String actionCancel() {
		return "cancel";
	}

	public String actionSaveSupplierMaster() {
		try {
			log.info("Started Saving supplier Information into database");
			TransactionStatus transactionStatus = supplierMasterService
					.saveSupplierMaster(supplierMasterPojo);
			MessageBoardUtil.publishTransactionStatusMessage(transactionStatus,
					EnumActor.SUPPLIER, "messageBoardId", "",
					new Object[] { supplierMasterPojo.getCompanyName() });
			log.info("Completed saving supplier information into database sucessfully");
		} catch (BusinessException e) {
			log.error(e);
		}
		return "openSuppliersListScreen";
	}

	/**
	 * @return the commonService
	 */
	public CommonService getCommonService() {
		return commonService;
	}

	/**
	 * @return the countrySelectItemList
	 */
	public List<SelectItem> getCountrySelectItemList() {
		return countrySelectItemList;
	}

	/**
	 * @return the supplierMasterPojo
	 */
	public SupplierMasterPojo getSupplierMasterPojo() {
		return supplierMasterPojo;
	}

	/**
	 * @return the supplierMasterService
	 */
	public SupplierMasterService getSupplierMasterService() {
		return supplierMasterService;
	}

	public void loadAllCountries() {
		try {
			List<CountryMasterPojo> countryMasterPojoList = commonService
					.getAllCountries(new CountryMasterPojo());
			List<SelectItem> countrySelectItemList = SelectItemUtils
					.convertCountryToSelectItems(countryMasterPojoList);
			setCountrySelectItemList(countrySelectItemList);
		} catch (BusinessException e) {
			e.printStackTrace();
		}

	}

	/**
	 * @param commonService
	 *            the commonService to set
	 */
	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}

	/**
	 * @param countrySelectItemList
	 *            the countrySelectItemList to set
	 */
	public void setCountrySelectItemList(List<SelectItem> countrySelectItemList) {
		this.countrySelectItemList = countrySelectItemList;
	}

	/**
	 * @param supplierMasterPojo
	 *            the supplierMasterPojo to set
	 */
	public void setSupplierMasterPojo(SupplierMasterPojo supplierMasterPojo) {
		this.supplierMasterPojo = supplierMasterPojo;
	}

	/**
	 * @param supplierMasterService
	 *            the supplierMasterService to set
	 */
	public void setSupplierMasterService(
			SupplierMasterService supplierMasterService) {
		this.supplierMasterService = supplierMasterService;
	}
}
